package com.itheima.untils;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class ModeUtils {

        private static final byte[] IV_VALUE = {12, 24, 48, 36, 48, 60, 72, 84};
        private static final byte[] KEY = {0X64, 0X6A, 0X71, 0X68, 0X2E, 0X63, 0X6F, 0X6D};


        public static String encryptDES (String str) throws Exception {
            IvParameterSpec zeroIv = new IvParameterSpec(IV_VALUE);
            SecretKeySpec key = new SecretKeySpec(KEY, "DES");
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
            byte[] encryptedData = cipher.doFinal(str.getBytes("UTF-8"));
            return new BASE64Encoder().encode(encryptedData);
        }


        public static String decryptDES (String miwen) throws Exception {
            byte[] byteMi = new BASE64Decoder().decodeBuffer(miwen);
            IvParameterSpec zeroIv = new IvParameterSpec(IV_VALUE);
            SecretKeySpec key = new SecretKeySpec(KEY, "DES");
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
            byte decryptedData[] = cipher.doFinal(byteMi);
            return new String(decryptedData, "UTF-8");
        }


    }

